"""
Case Type   : GUC
Case Name   : 使用set to方式修改max_active_global_temporary_table为10，观察预期结果
Create At   : 2021/3/15
Owner       : opentestcase001
Description :
    1、set方式修改max_active_global_temporary_table为10
    2、清理环境
Expect      :
    1、报错
    2、成功
History     :modified by opentestcase005,2022.09.13，参数类型变更，修改参数设置方式
             modified by wangtianjing,2024.7.2，同步sharestorage目录下优化后的用例
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class GucTestCase(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.comsh = CommonSH('PrimaryDbUser')
        self.common = Common()
        self.default_value = self.common.show_param(
            'max_active_global_temporary_table')

    def test_guc(self):
        text = '--step1:set方式修改max_active_global_temporary_table为10;' \
               'expect:报错--'
        self.log.info(text)
        set_cmd = f'set max_active_global_temporary_table to 10;'
        set_res = self.comsh.execut_db_sql(set_cmd)
        self.log.info(set_res)
        self.assertIn('ERROR:  parameter "max_active_global_temporary_table" '
                      'cannot be changed', set_res)

    def tearDown(self):
        text = '--step2:清理环境;expect:成功--'
        self.log.info(text)
        alter_cmd = self.comsh.execut_db_sql(
            f'alter system set max_active_global_temporary_table='
            f'{self.default_value};')
        self.log.info(alter_cmd)
        self.comsh.restart_db_cluster()
        status = self.comsh.get_db_cluster_status()
        self.assertTrue("Normal" in status or 'Degraded' in status,
                        '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
